<template>
  <div class="home-container">
    <!-- container布局容器 -->
    <el-container class="home-container">
      <!-- 头部区域 -->
      <el-header>
        <div>
          <a href="https://blog.csdn.net/kzb68up" target="_blank">
            <img
              src="../assets/work_cat.jpg"
              alt="Hello Vue!"
              title="我的技术博客"
            />
          </a>
          <span>电商后台管理系统</span>
        </div>

        <el-button type="info" @click="logout">退出</el-button>
      </el-header>
      <!-- 页面主体区域 -->
      <el-container>
        <!-- 左侧-侧边栏 -->
        <el-aside :width="toggleCollapse ? '64px' : '200px'">
             <div class="toggle-button" @click="toggleButton">|||</div>

             <el-menu background-color="#333744" text-color="#fff" active-text-color="#409BFF" :unique-opened="true" :collapse="toggleCollapse" :collapse-transition="false" router :default-active="activePath">
               <!-- 一级菜单 -->
               <el-submenu :index="item.id+''" v-for="item in menulist" :key="item.id">

                 <!-- 一级菜单-模板区域 -->
                 <template slot="title">
                   <!-- 图标 -->
                   <i :class="iconObj[item.id]"></i>
                   <!-- 文本内容 -->
                   <span>{{item.authName}}</span>
                 </template>

                   <!-- 二级菜单 -->
                   <el-menu-item :index="'/'+subItem.path" v-for="subItem in item.children" :key="subItem.id" @click="saveNavState('/'+subItem.path)">
                     <template slot="title">
                        <!-- 图标 -->
                       <i class="el-icon-menu"></i>
                       <!-- 文本内容 -->
                      <span>{{subItem.authName}}</span>
                     </template>
                   </el-menu-item>
               </el-submenu>
             </el-menu>
        </el-aside>
        <!-- 右侧-内容区域 -->
        <el-main>
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script>
export default {
  created () {
    this.getMenuList()
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  data () {
    return {
      // 左侧菜单数据
      menulist: [],
      iconObj: {
        // 用户管理
        125: 'iconfont icon-user',
        // 权限管理
        103: 'iconfont icon-tijikongjian',
        // 商品管理
        101: 'iconfont icon-shangpin',
        // 订单管理
        102: 'iconfont icon-danju',
        // 数据统计
        145: 'iconfont icon-baobiao'
      },
      // 菜单栏展开折叠
      toggleCollapse: false,
      // 被激活的链接地址
      activePath: ''
    }
  },
  methods: {
    // 退出登录
    logout: function () {
      // window.sessionStorage.removeItem('token')
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    // 获取左侧菜单权限
    async getMenuList () {
      const { data: res } = await this.$http.get('/menus')
      if (res.meta.status !== 200) {
        return this.$message.error(res.meta.msg)
      } else {
        this.menulist = res.data
      }
    },
    // 菜单栏展开折叠
    toggleButton () {
      this.toggleCollapse = !this.toggleCollapse
    },
    // 二级菜单选中高亮
    saveNavState (activePath) {
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
    }
  }
}
</script>

<style lang="less" scoped>
.home-container {
  height: 100%;
}
.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  align-items: center;
  color: #fff;
  font-size: 20px;
}
.el-header div {
  height: 100%;
  width: 270px;
  display: flex;
  align-items: center;
  span {
    margin-left: 15px;
    text-shadow: 0 0.5px 1px #eaedf1;
    -webkit-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
  }
  a {
    display: inline-block;
    height: 100%;
    img {
      height: 100%;
      width: auto;
      border-radius: 50%;
      border: 0.5px solid #eaedf1;
      padding: 0.5px;
      box-sizing: border-box;
      //设置图片不可拖动
      -webkit-user-drag: none;
    }
  }
}
.el-aside {
  background-color: #333744;
  .el-menu {
    border-right: none;
  }
}
.el-main {
  background-color: #eaedf1;
}
.iconfont {
  margin-right: 10px;
}
.toggle-button {
  background-color: #4a5064;
  font-size: 10px;
  line-height: 24px;
  color: #fff;
  text-align: center;
  cursor: pointer;
  letter-spacing: 3px;
}
</style>
